import React from 'react';
import { Upload, Button } from 'antd';
import axios from 'axios';

class Cs extends React.Component {
  
  beforeUpload = (file) => {
    const formData = new FormData();
    formData.append('file', file);
    
    axios.post('https://api.leancloud.cn/1.1/files/file', formData, {
      headers: {
        " X-LC-Id": "r5CTN9z96pCW8mI8fx9YxOmw-gzGzoHsz",
        "X-LC-Key": "8gSsl4Sz0ILMiQeMNMA8xQqg",
        'Content-Type': 'image/jpeg'
      }
    }).then(response => {
      console.log(response.data);
      axios.post('https://api.leancloud.cn/1.1/classes/image',{
        image_id: this.props.objectId ,
        image_url: response.data.url
    },
      {
        headers: {

          " X-LC-Id": "r5CTN9z96pCW8mI8fx9YxOmw-gzGzoHsz",
          "X-LC-Key": "8gSsl4Sz0ILMiQeMNMA8xQqg",
          'Content-Type': 'application/json',
      }
      }    
    ).then((res) => {
      window.location.reload()
  })
    }).catch(error => {
      console.log(error);
    });
    
    return false; // 阻止默认的上传行为
  }
  
  render() {
    // const objectId = this.props.objectId
    return (
      <div>
        <Upload
          action=""
          beforeUpload={this.beforeUpload}
        >
          <Button>选择图片</Button>
        </Upload>
      </div>
    );
  }
}

export default Cs